package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.UserSoulInfoApi;
import com.tanhua.domain.mongo.Options;
import com.tanhua.domain.mongo.Questionnaire;
import com.tanhua.domain.mongo.Subject;
import com.tanhua.domain.mongo.UserSoulInfo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class UserSoulInfoApiImpl implements UserSoulInfoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserSoulInfo getSoulInfo(Long id) {
        Query query = new Query(Criteria.where("userId").is(id));
        UserSoulInfo userSoulInfo = mongoTemplate.findOne(query, UserSoulInfo.class);
        //是否为第一次答题
        if (userSoulInfo == null){
            userSoulInfo = new UserSoulInfo();
            userSoulInfo.setId(new ObjectId());
            userSoulInfo.setUserId(id);
            userSoulInfo.setLevel("初级");
            mongoTemplate.insert(userSoulInfo,"user_soul_info");


        }

        return userSoulInfo;
    }

    @Override
    public void setReportId(Long id, String reportId) {
        Query query = new Query(Criteria.where("userId").is(id));
        Update update = new Update();
        update.set("reportId",reportId);
        mongoTemplate.upsert(query,update,UserSoulInfo.class);
    }

    @Override
    public List<UserSoulInfo> findSimilar(String reportId) {
        Query query = new Query(Criteria.where("reportId").is(reportId));
        List<UserSoulInfo> userSoulInfoList = mongoTemplate.find(query, UserSoulInfo.class);
        return userSoulInfoList;
    }

    @Override
    public void setLevel(Long id, String level) {
        Query query = new Query(Criteria.where("userId").is(id));
        Update update = new Update();
        update.set("level",level);
        mongoTemplate.upsert(query,update,UserSoulInfo.class);
    }
}
